<style>
.il2 { display: inline-block !important; width: 293px; }
.pure_table_extra_ab { width: 293px; border: 1px solid #aaa !important; }
.pure_table_extra_ab td { height: 20px !important; }
.pure_table_extra_ab a { color: #666; }
.pure_table_extra_ab * { font-size: 12px !important; font-family: Trebuchet MS !important; }
.pure_table_extra_ab .date { float: right; color: #666; font-size: 11px !important; }

.pure_form_extra2 { padding: 10px; }
.pure_form_extra2 b { font-size: 12px !important; color: #666; font-family: Trebuchet MS; }
.pure_form_extra { background: #fff; padding: 20px 30px; border: 1px solid #aaa; border-radius: 3px; }
.pure_form_extra a { font-size: 13px !important; color: #666; font-family: Trebuchet MS; }
.img { background: #fff; width: 75px; padding: 5px 5px 15px 5px; margin: 0 2px; border: 1px solid #666; display: inline-block; }
.img img { cursor: pointer; }
.img td { font-size: 11px !important; }
.img i { cursor: pointer !important; }
</style>

<?php
$id = Utils::get_param('id', 0);
$search = Utils::get_param('search', '');

$opts = new stdClass();
$opts->cd = 't.id = :v1';
$opts->cdv = array(':v1' => $id);
$data = $orm->get('pb_ht_article', NULL, NULL, NULL, NULL, $opts)->d;

if (count($data) == 0) {
  return;
}

$data[0]->__images = array();
$data[0]->__downloads = array();

$opts = new stdClass();
$opts->cd = 't.article_track_id = :v1';
$opts->cdv = array(':v1' => $data[0]->track_id);
$files = $orm->get('pb_ht_article_file', NULL, NULL, NULL, NULL, $opts)->d;
foreach ($files as $itm) {
  if ($itm->is_image == 1) {
    $data[0]->__images[] = $itm;
  } else {
    $data[0]->__downloads[] = $itm;
  }
}

$data[0]->visited__ += 1;
$orm->update('pb_ht_article', $data[0]);

$cat = $data[0]->category_id;

$categories = $orm->get('pb_ht_category', NULL, NULL, NULL, NULL, NULL)->d;
foreach ($categories as $itm) {
  if (strpos($data[0]->categories__, ",$itm->id,") !== false) {
    $itm->visited__ += 1;
    $orm->update('pb_ht_category', $itm);
  }
}
include dirname(__FILE__) . '/../admin_articles/index.srv.php';

$root = new stdClass();
$root->id = 0;
$root->name = '所有栏目';
$root->parent_id = -1;
Server::__create_root($categories, $root);

$rslt = new stdClass();
$rslt->path = array();
__path($root, $cat, $rslt);
if (count($rslt->path) == 0) {
  return;
}

$rslt->children = array();
__children($root, $cat, $rslt);

function __path($node, $cat, $rslt) {
  if ($node->id == $cat) {
    $obj = clone $node;
    unset($obj->__c);
    array_push($rslt->path, $obj);
    return true;
  }
  if ($node->id != 0 || true) {
    $obj = clone $node;
    unset($obj->__c);
    array_push($rslt->path, $obj);
  }
  foreach ($node->__c as $itm) {
    if (__path($itm, $cat, $rslt) == true) {
      return true;
    }
  }
  if ($node->id != 0 || true) {
    array_pop($rslt->path);
  }
}

function __children($node, $cat, $rslt) {
  if ($node->id == $cat) {
    $rslt->children =  $node->__c;
    return;
  }
  foreach ($node->__c as $itm) {
    if ($itm->id == $cat) {
      $rslt->children = &$itm->__c;
      return;
    }
  }
}
?>

<?php if (isset($rslt->path[1]->ad_top) && $rslt->path[1]->ad_top != '') { ?>
<img src="<?php echo $rslt->path[1]->ad_top; ?>" alt="" style="width: 950px;"></img>
<?php } ?>
<table style="width: 950px; margin-top: 5px;" cellpadding=0 cellspacing=0>
  <tr>
    <td style="vertical-align: top; padding-right: 10px;">
      <div class="pure-form pure_form_extra" style="margin-bottom: 10px;">
        <b>您的位置</b>
        <?php foreach ($rslt->path as $itm) { ?>
          <i class="fa fa-arrow-right"></i>
          <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&cat=<?php echo $itm->id; ?>&search=<?php echo urlencode($search); ?>');"><?php echo $itm->name; ?></a>
        <?php } ?>
      </div>
      
      <div id="my_art" class="pure-form pure_form_extra" style="width: 620px; min-height: 550px;">
        <legend><?php echo $data[0]->title; ?>&nbsp;<span><?php echo $data[0]->date; ?></span></legend>
        <?php if (count($data[0]->__images) > 0) { ?>
        <div id="photo_gal">
        <script type="text/javascript">
          var p = document.getElementById('photo_gal');
          var images = <?php echo json_encode($data[0]->__images); ?>;
          for(var i = 0; i < images.length; i++) {
            var div = document.createElement('div');
            div.className = 'panel_gal';
            var img = document.createElement('img');
            img.src = images[i].link;
            div.appendChild(img);
            p.appendChild(div);
          }
          
          var ul = document.createElement('ul');
          ul.className = 'filmstrip_gal';
          for(var i = 0; i < images.length; i++) {
            var li = document.createElement('li');
            var img = document.createElement('img');
            img.src = images[i].link;
            li.appendChild(img);
            ul.appendChild(li);
            p.appendChild(ul);
          }
        </script>
        </div>
        <div>&nbsp;</div>
        <?php } ?>
        <div style="text-align: justify; letter-spacing: 1px"><?php echo $data[0]->content; ?></div>
        <div>&nbsp;</div>
        <?php if (count($data[0]->__downloads) > 0) { ?>
        <div class="pure_form_extra2 ui-state-highlight ui-corner-all">
          <b>下载区</b>
          <div id="downloads"></div>
        </div>
        <div>&nbsp;</div>
        <?php } ?>
      </div>
    </td>
    <?php if ($cat != 0) { ?>
    <td style="width: 230px; vertical-align: top;">
      <div id="ad_right" class="pure-form pure_form_extra" style="width: 200px; margin-bottom: 10px; min-height: 600px; overflow: hidden;">
        <legend style="font-size: 13px; font-weight: bold;">广告栏</legend>
        <?php echo (isset($rslt->path[1]->ad_right) && $rslt->path[1]->ad_right != '') ? $rslt->path[1]->ad_right : ''; ?>
      </div>
    </td>
    <?php } ?>
    <tr><td colspan="2"><?php include PLGN_DIR . basename(dirname(__FILE__)) . '/inc.bottom.php'; ?></td></tr>
  </tr>
</table>
<div>&nbsp;</div>
<?php if (isset($rslt->path[1]->ad_bottom) && $rslt->path[1]->ad_bottom != '') { ?>
<img src="<?php echo $rslt->path[1]->ad_bottom; ?>" alt="" style="width: 950px; margin-top: 10px;"></img>
<?php } ?>
<div>&nbsp;</div>


<link rel="stylesheet" href="../3rd_party/__gvq/global.css">
<script type="text/javascript" src="../3rd_party/__gvq/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="../3rd_party/__gvq/jquery.galleryview1.js"></script>
<script type="text/javascript" src="../3rd_party/__gvq/jquery.timers-1.1.2.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
    var pwidth = 600;
    var pheight = 400;
    var uniqueId = 'gal';
    var fwidth = (pwidth - 50) / 6;
    var fheight = pheight / 7;
    $('.panel_'+uniqueId+' img').css({'width': pwidth + 'px', 'height': pheight + 'px'});
    $('.filmstrip_'+uniqueId+' img').css({'width': fwidth + 'px','height': fheight + 'px'});
    $('#photo_'+uniqueId).galleryView({
      panel_width: pwidth,
      panel_height: pheight * 6/7-4,
      frame_width: fwidth,
      frame_height: fheight,
      unique: uniqueId,
      pre_nex_btn: true
    });
  });
</script>




<script type="text/javascript">
  var __cat = '<?php echo $cat; ?>';
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __dlurl = '<?php echo Utils::get_dlurl($app_data); ?>';

  var __authorise_required = '<?php echo ($data[0]->category_id == USERONLY) ? 1 : 0; ?>';
  var __authorised = '<?php echo (isset($_SESSION[SYS . $app_data->sys->php_name]->is_authorised) && $_SESSION[SYS . $app_data->sys->php_name]->is_authorised == 1) ? 1 : 0; ?>';

  
  $(function(){
    artgal();
    
    __browsed = Cookie.get_cookie('a_browsed');
    if (!__browsed) {
      __browsed = [{
        id: __data[0].id,
        title: __data[0].title,
        date: __data[0].date,
        __cat: __cat
      }];
    } else {
      __browsed = JSON.parse(__browsed);
      Utils.splice_by_id(__data[0].id, __browsed);
      __browsed.splice(0, 0, {
        id: __data[0].id,
        title: __data[0].title,
        date: __data[0].date,
        __cat: __cat
      });
      if (__browsed.length > 9) {
        __browsed.splice(8, __browsed.length - 9);
      }
    }
    Cookie.set_cookie('a_browsed', JSON.stringify(__browsed), 1);
    
    render_downloads();
  });

  function render_downloads() {
    $('#downloads').empty();
    for (var idx in __data[0].__downloads) {
      var itm = __data[0].__downloads[idx];
      $('#downloads').append(
          '<table cellpadding=0 cellspacing=0 class="img">' +
            '<tr><td style="text-align: right;"><i class="fa fa-times" onclick="delete_file(\'' + itm.id + '\');"></i></td></tr>' +
            '<tr><td title="' + itm.name + '" style="text-align: center; font-family: Trebuchet MS !important;">' + Utils.get_short(itm.name, 8) + '</td></tr>' +
            '<tr><td title="' + itm.name + '" style="text-align: center;"><i class="fa fa-file fa-3x" onclick="dl(\'' + itm.link + '\', \'' + itm.name + '\')"></i></td></tr>' +
          '</table>');
    }
  };

  function dl(link, name) {
    if (__authorise_required == 1 && __authorised == 0) {
      Utils.block_ui(null);
      location.href = __dlurl + '&p=' + Utils.encodeURI(link) + '&n=' + Utils.encodeURI(name);
    } else {
      window.open(__dlurl + '&p=' + Utils.encodeURI(link) + '&n=' + Utils.encodeURI(name));
    }
  }
</script>